package com.dong.cutesms.activitys;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dong.cutesms.R;
import com.dong.cutesms.components.DialogActionSMS;
import com.dong.cutesms.components.HorizontalImageAdapter;
import com.dong.cutesms.components.HorizontalView;
import com.dong.cutesms.constant.AppParameters;
import com.dong.cutesms.constant.SmsTable;
import com.dong.cutesms.models.PeopleModel;
import com.dong.cutesms.models.Sms;
import com.dong.cutesms.models.SmsModel;
import com.dong.cutesms.utils.SmsUtils;

public class ShowMainSmsActivity extends Activity {
	private List drawables;
	private List<SmsModel> smsList;
	private HorizontalImageAdapter imageAdapter;
	private HorizontalView listView;
	private SmsUtils smsUtils;
	private final Context context = this;
	private boolean firstTouch;
	private int touchIndex;
	private ImageButton sendsms;
	private EditText et_message;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.activity_horizontal_chat_region);
		
		setupActivity();
		setupUI();
		
	}
	
	private void setupActivity(){
		ContentResolver contentResolver = getContentResolver();
		smsUtils = new SmsUtils(contentResolver, getApplicationContext());
		AppParameters.smsList = smsUtils.getSmsList(AppParameters.people);
		
		updatePeople();
	}
	
	private void updatePeople(){
		PeopleModel people = AppParameters.people;
		if(people.hasSms>0){
			people.hasSms = 0;
			smsUtils.updatePeople(people);
		}
		
	}
	
	private void setupUI() {
		listView = (HorizontalView) findViewById(R.id.gallery);
		imageAdapter = new HorizontalImageAdapter(this, drawables, smsList);
		sendsms = (ImageButton) findViewById(R.id.btn_send_sms);
		et_message = (EditText) findViewById(R.id.et_message);
		sendsms.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String message = et_message.getText().toString();
				if(message.equals("")){
					return;
				}
				String phoneNumber = AppParameters.people.phoneNumber;
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(phoneNumber, null, message, null, null);
				et_message.setText("");
				
				int threadID = AppParameters.people.threadID;
				String address= AppParameters.people.phoneNumber;
				long date = System.currentTimeMillis();
				int read = SmsTable.MESSAGE_IS_READ;
				int type = SmsTable.MESSAGE_TYPE_SENT;
				String body = message;
				Sms newSms = new Sms(threadID, address, date, read, type, body.trim());
				Uri sentsms = smsUtils.push2SmsTable(newSms);
				
				int sendID = smsUtils.getIDRecentSentSms(sentsms);
				if(sendID!=-1){
					SmsModel sms = new SmsModel(phoneNumber,AppParameters.people.displayName, message,sendID, 2);
					AppParameters.smsList.add(0,sms);
				}
				
				imageAdapter.notifyDataSetChanged();
			}
		});
		
		listView.setAdapter(imageAdapter);
		
		firstTouch = true;
		touchIndex = 0;
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(final AdapterView<?> adapterView,
					final View view, final int position, final long id) {
				System.out.println("listview OnLongClick");
				String displayName = AppParameters.smsList.get(position).displayName;
				final Dialog dialog = new DialogActionSMS(context).getInstance();
				TextView title = (TextView)dialog.findViewById(R.id.confirm_title);
				title.setText(displayName);
				LinearLayout btn_deletesms = (LinearLayout) dialog.findViewById(R.id.btn_deletesms);
				btn_deletesms.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						System.out.println("onclick delete message");
						smsUtils.deleteSMS((int) id);
						//adapterView.removeViewInLayout(view);
						
						AppParameters.smsList.remove(position);
						new Thread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								try {
									Thread.sleep(300);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								ShowMainSmsActivity.this.runOnUiThread(new Runnable() {
									
									@Override
									public void run() {
										// TODO Auto-generated method stub
										imageAdapter.notifyDataSetChanged();
									}
								});
								
							}
						}).start();
						
						dialog.dismiss();
					}
				});
				
				
				return false;
			}
		});

	}
	
	private void scrollMyListViewToBottom() {
	    listView.post(new Runnable() {
	        @Override
	        public void run() {
	            // Select the last row so it will scroll into view...
	        	listView.setSelection(listView.getCount() - 1);
	        }
	    });
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

}
